clear all; close all; clc;

output_path = "toyFigures/";

NNx    =   5001;
minX   =   -0.5; 
maxX   =   0.5;  
incX   =   (maxX-minX)/(NNx-1);
vecX   =   minX:incX:maxX;

%Calculate normal pdf
stDev = 0.1;
normal_pdf    =   NaN(1,NNx);
for ii=1:NNx
    normal_pdf(1,ii)=normpdf(vecX(1,ii),0,stDev);
end
NNxhalf = (NNx-1)/2+1;
xbarminus = (normal_pdf(NNxhalf+1:end)*vecX(1,NNxhalf+1:end)')/sum(normal_pdf(NNxhalf+1:end))-0.015;
              

minY    =   0;
maxY    =   5;
minYY   =   0;
maxYY   =   1.25;
norm_thresh   =   0.125;
calvo   = 0.25;

minXPosInd = (NNx-501)/2;
vecXPosInd = (minXPosInd:NNx);
vecXPosIndRev = (NNx:-1:minXPosInd);
minXPos  = vecX(minXPosInd);

fig=figure(1);
setfontsize;
%plot(vecX,normal_pdf,'k','LineWidth',2);
p1=fill([vecX(vecXPosInd) vecX(minXPosInd)],[normal_pdf(vecXPosInd) 0],[0.85 0.85 0.85],'LineStyle','none'); xlabel('Lagged price gap'); axis([minXPos maxX minY maxY]); set(gcf, 'Color', 'w'); title('','interpreter','tex');  ylabel('Density'); title('(S,s) pricing','Interpreter','latex');
hold on;
text(0.0001,4.2,'Gap density','Color',[0.85 0.85 0.85]);
hold on;
p2=fill(vecX(vecXPosInd),(vecX(vecXPosInd)~=vecX(1,minXPosInd)).*(vecX(vecXPosInd)~=vecX(1,NNx)).*normal_pdf(vecXPosInd).*(vecX(vecXPosInd)>norm_thresh),[0.1 0.1 0.1],'LineStyle','none'); axis([minXPos maxX minY maxY]);
hold on;
text(0.2,1,'Price decreases','Color',[0.01 0.01 0.01]);
hold on;
text(0.25,0.5,'are large','Color',[0.01 0.01 0.01]);
hold on;
plot([0 0],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5); 
hold on;
yyaxis right; p3=plot(vecX(vecXPosInd),(vecX(vecXPosInd)>norm_thresh)*1,'Color',[0.4940 0.1840 0.5560]); axis([minXPos maxX minYY maxYY]); ylabel('Decrease probability');
hold on;
text(0.155,0.9,'Decrease probability','Color',[0.4940 0.1840 0.5560]);
hold on;
plot([norm_thresh norm_thresh],[0 1],'Color',[0.4940 0.1840 0.5560]);

set(gcf,'Position',[100 100 400 400]);
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print(fig, output_path + "StateDependence_Ss.pdf", '-dpdf');
print(fig, output_path + "StateDependence_Ss.eps", '-depsc');

fig=figure(2);
setfontsize;
p1=fill([vecX(vecXPosInd) vecX(minXPosInd)],[normal_pdf(vecXPosInd) 0],[0.85 0.85 0.85],'LineStyle','none'); xlabel('Lagged price gap'); axis([minXPos maxX minY maxY]); set(gcf, 'Color', 'w'); title('','interpreter','tex'); ylabel('Density'); title('Calvo pricing','Interpreter','latex') 
hold on;
text(0.0001,4.2,'Gap density','Color',[0.85 0.85 0.85]);
hold on;
p2=fill(vecX(vecXPosInd),(vecX(vecXPosInd)~=vecX(1,minXPosInd)).*(vecX(vecXPosInd)~=vecX(1,NNx)).*(calvo*normal_pdf(vecXPosInd)).*(vecX(vecXPosInd)>=0),[0.1 0.1 0.1],'LineStyle','none'); axis([minXPos maxX minY maxY]);
hold on;
text(0.1,0.75,'Price decreases','Color',[0.01 0.01 0.01]);
hold on;
text(0.25,0.35,'are smaller','Color',[0.01 0.01 0.01]);
hold on;
plot([0 0],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5); 
yyaxis right; p3=plot(vecX(vecXPosInd),(vecX(vecXPosInd)>0)*calvo,'Color',[0.4940 0.1840 0.5560]); axis([minXPos maxX minYY maxYY]); ylabel('Decrease probability');
hold on;
text(0.15,0.325,'Decrease probability','Color',[0.4940 0.1840 0.5560]);
plot([0 0],[0 calvo],'Color',[0.4940 0.1840 0.5560]);

set(gcf,'Position',[100 100 400 400]);
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print(fig, output_path + "StateDependence_Calvo.pdf", '-dpdf');
print(fig, output_path + "StateDependence_Calvo.eps", '-depsc');


fig= figure(3);
setfontsize;
slopeProbability = 2;
interceptProbability = 0.25;
set(fig,'defaultAxesColorOrder',[[0.01 0.01 0.01]; [0.4940 0.1840 0.5560]]);
%plot(vecX,normal_pdf,'k','LineWidth',2);
p1=fill([vecX(vecXPosInd) vecX(minXPosInd)],[normal_pdf(vecXPosInd) 0],[0.85 0.85 0.85],'LineStyle','none'); xlabel('Lagged price gap'); axis([minXPos maxX minY maxY]); set(gcf, 'Color', 'w'); title('','interpreter','tex');  ylabel('Density'); title('Linear hazard','Interpreter','latex');
hold on;
text(0.0001,4.2,'Gap density','Color',[0.85 0.85 0.85]);
hold on;
funDecreaseHazard = min((vecX(vecXPosInd)>0).*(interceptProbability+vecX(vecXPosInd)*slopeProbability),1);
p3=fill(vecX(vecXPosInd),(vecX(vecXPosInd)~=vecX(1,minXPosInd)).*(vecX(vecXPosInd)~=vecX(1,NNx)).*normal_pdf(vecXPosInd).*funDecreaseHazard,[0.1 0.1 0.1],'LineStyle','none'); axis([minXPos maxX minY maxY]);
hold on;
text(0.15,1,'Price decreases have','Color',[0.01 0.01 0.01]);
hold on;
text(0.2,0.5,'intermediate size','Color',[0.01 0.01 0.01]);
hold on;
yyaxis right; p2=plot(vecX(vecXPosInd),funDecreaseHazard,'Color',[0.4940 0.1840 0.5560]); axis([minXPos maxX minYY maxYY]); ylabel('Decrease probability');
hold on;
text(0.1,0.51,'Decrease probability','Color',[0.4940 0.1840 0.5560],'rotation',44);
hold on;
plot([0 0],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5,'LineStyle','-'); 

set(gcf,'Position',[100 100 400 400]);
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print(fig, output_path + "StateDependence_linear.pdf", '-dpdf');
print(fig, output_path + "StateDependence_linear.eps", '-depsc');


%close all;
fig=figure(4);
setfontsize;
shock_size_toy = 0.015;
p1=fill([vecX(vecXPosInd) vecX(minXPosInd)],[normal_pdf(vecXPosInd) 0],[0.85 0.85 0.85],'LineStyle','none'); axis([minXPos maxX minY maxY]); set(gcf, 'Color', 'w'); xlabel('Lagged price gap'); ylabel('Density'); title('(S,s) w/ tightening','Interpreter','latex');
hold on;
text(0.0001,4.2,'Gap density','Color',[0.85 0.85 0.85]);
hold on;
text(xbarminus-0.02,-0.2,'$\bar{x}^-$','Color',[0.01 0.01 0.01],'Interpreter','latex');
hold on;
%plot(vecX+shock_size_toy,mixed_pdf,'k','LineWidth',2); axis([minX maxX y_min_mixed y_max_mixed]); set(gcf, 'Color', 'w'); title('Large positive shock','FontWeight','Normal');
            %subplot(1,3,2); plot(x_vec+infl_toy+shock_size_toy,mixed_pdf,'k','LineWidth',2); axis([x_min x_max y_min_mixed y_max_mixed]); set(gca,'TickLabelInterpreter','latex'); set(gcf, 'Color', 'w'); title('Large positive shock','FontWeight','Normal');
p2=fill(vecX(vecXPosInd),(vecX(vecXPosInd)~=vecX(1,minXPosInd)).*(vecX(vecXPosInd)~=vecX(1,NNx)).*normal_pdf(vecXPosInd).*(vecX(vecXPosInd)>=norm_thresh-shock_size_toy).*(vecX(vecXPosInd)<norm_thresh),[0.1 0.1 0.1],'LineStyle','none'); axis([minXPos maxX minY maxY]);
hold on;
text(0.13,2,'New decreases both far','Color',[0.01 0.01 0.01]);
hold on;
text(0.15,1.6,'from 0: broad','Color',[0.01 0.01 0.01]);
hold on;
text(0.33,1.2,'selection','Color',[0.01 0.01 0.01]);
hold on;
text(0.15,0.8,'from $\bar{x}^-$: narrow','Color',[0.01 0.01 0.01]);
hold on;
text(0.33,0.4,'selection','Color',[0.01 0.01 0.01]);
hold on;
plot([0 0],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5);
hold on;
yyaxis right; p3=plot(vecX(vecXPosInd),(vecX(vecXPosInd)>norm_thresh-shock_size_toy)*1,'Color',[0.4940 0.1840 0.5560]); axis([minXPos maxX minYY maxYY]); ylabel('Decrease probability');
hold on;
text(0.12,0.9,'$\Leftarrow$ Decrease probability','Color',[0.4940 0.1840 0.5560]);
hold on;
text(0.12,0.8,'$\Leftarrow$ shifts to the left','Color',[0.4940 0.1840 0.5560]);
hold on;
plot([norm_thresh-shock_size_toy norm_thresh-shock_size_toy],[0 1],'Color',[0.4940 0.1840 0.5560]);
hold on;
plot([norm_thresh norm_thresh],[0 1],'--','Color',[0.8633    0.6250    0.8633]);
hold on;
plot([xbarminus xbarminus],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5,'LineStyle','-'); 

%legend([p1,p2,p3],'Density','New price decreases','Decrease hazard');

set(gcf,'Position',[100 100 400 400]);
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print(fig, output_path + "SelectionFigure_Ss.pdf", '-dpdf');
print(fig, output_path + "SelectionFigure_Ss.eps", '-depsc');


fig=figure(5);
setfontsize;
p1=fill([vecX(vecXPosInd) vecX(minXPosInd)],[normal_pdf(vecXPosInd) 0],[0.85 0.85 0.85],'LineStyle','none'); axis([minXPos maxX minY maxY]); set(gcf, 'Color', 'w'); xlabel('Lagged price gap'); title('','interpreter','tex'); ylabel('Density'); title('Calvo w/ tightening','Interpreter','latex');
hold on;
text(0.0001,4.2,'Gap density','Color',[0.85 0.85 0.85]);
hold on;
text(xbarminus-0.02,-0.2,'$\bar{x}^-$','Color',[0.01 0.01 0.01],'Interpreter','latex');
hold on;
p2=fill(vecX(vecXPosInd),(vecX(vecXPosInd)~=vecX(1,minXPosInd)).*(vecX(vecXPosInd)~=vecX(1,NNx)).*(calvo*normal_pdf(vecXPosInd)).*(vecX(vecXPosInd)>=-shock_size_toy).*(vecX(vecXPosInd)<0),[0.85 0.85 0.85],'LineStyle','none'); axis([minXPos maxX minY maxY]);
hold on;
%text(0.01,0.75,'New decreases at zero','Color',[0.01 0.01 0.01]);
text(0.01,0.75,'No new decreases ($>0$ gaps)','Color',[0.01 0.01 0.01]);
hold on;
text(0.01,0.25,'(no selection, no ext. margin)','Color',[0.01 0.01 0.01]);
hold on;
plot([0 0],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5); 
yyaxis right; p3=plot(vecX(vecXPosInd),(vecX(vecXPosInd)>-shock_size_toy)*calvo,'Color',[0.4940 0.1840 0.5560]); axis([minXPos maxX minYY maxYY]); ylabel('Decrease probability');
hold on;
text(0.01,0.425,'$\Leftarrow$ Decrease probability','Color',[0.4940 0.1840 0.5560]);
text(0.01,0.325,'$\Leftarrow$ shifts to the left','Color',[0.4940 0.1840 0.5560]);
plot([0-shock_size_toy 0-shock_size_toy],[0 calvo],'Color',[0.4940 0.1840 0.5560]);
hold on;
plot([xbarminus xbarminus],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5,'LineStyle','-'); 

set(gcf,'Position',[100 100 400 400]);
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print(fig, output_path + "SelectionFigure_Calvo.pdf", '-dpdf');
print(fig, output_path + "SelectionFigure_Calvo.eps", '-depsc');

%close all;
fig = figure(6);
slopeProbability = 2;
interceptProbability = 0.25;
setfontsize;
set(fig,'defaultAxesColorOrder',[[0.01 0.01 0.01]; [0.4940 0.1840 0.5560]]);
%plot(vecX,normal_pdf,'k','LineWidth',2);
p1=fill([vecX(vecXPosInd) vecX(minXPosInd)],[normal_pdf(vecXPosInd) 0],[0.85 0.85 0.85],'LineStyle','none'); xlabel('Lagged price gap'); axis([minXPos maxX minY maxY]); set(gcf, 'Color', 'w'); title('','interpreter','tex');  ylabel('Density'); title('Linear hazard w/ tightening','Interpreter','latex');
hold on;
text(0.0001,4.2,'Gap density','Color',[0.85 0.85 0.85]);
hold on;
text(xbarminus-0.02,-0.2,'$\bar{x}^-$','Color',[0.01 0.01 0.01],'Interpreter','latex');
hold on;
funDecreaseHazardwShock = min((vecX(vecXPosInd)>-shock_size_toy).*(interceptProbability+(shock_size_toy*slopeProbability)+vecX(vecXPosInd)*slopeProbability),1);
funDecreaseHazardDiff = (vecX(vecXPosInd)>0).*(funDecreaseHazardwShock-funDecreaseHazard);
p3=fill(vecX(vecXPosInd),(vecX(vecXPosInd)~=vecX(1,minXPosInd)).*(vecX(vecXPosInd)~=vecX(1,NNx)).*normal_pdf(vecXPosInd).*funDecreaseHazardDiff,[0.1 0.1 0.1],'LineStyle','none'); axis([minXPos maxX minY maxY]);
hold on;
text(0.07,1.25,'More new decreases','Color',[0.01 0.01 0.01]);
hold on;
text(0.09,0.95,'(gross extensive margin)','Color',[0.01 0.01 0.01]);
hold on;
text(0.07,0.5,'Dispersed new decreases','Color',[0.01 0.01 0.01]);
hold on;
text(0.09,0.2,'(avg$\approx\bar{x}^-$, no narrow sel.)','Color',[0.01 0.01 0.01]);
hold on;
plot([0 0],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5); 
yyaxis right; p2=plot(vecX(vecXPosInd),funDecreaseHazardwShock,'Color',[0.4940 0.1840 0.5560]); axis([minXPos maxX minYY maxYY]); ylabel('Decrease probability');
hold on;
plot(vecX(vecXPosInd),funDecreaseHazard,'--','Color',[0.8633    0.6250    0.8633]);
hold on;
text(0.09,0.5325,'$\Leftarrow$ ','Color',[0.4940 0.1840 0.5560]);
hold on;
text(0.115,0.565,'Decrease probability','Color',[0.4940 0.1840 0.5560],'rotation',44);
hold on;
text(0.15,0.51,'shifts to the left','Color',[0.4940 0.1840 0.5560],'rotation',44);
hold on;
plot([xbarminus xbarminus],[minY maxY],'Color',[0.5 0.5 0.5],'LineWidth',0.5,'LineStyle','-'); 


set(gcf,'Position',[100 100 400 400]);
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];
print(fig, output_path + "SelectionFigure_linear.pdf", '-dpdf');
print(fig, output_path + "SelectionFigure_linear.eps", '-depsc');

